import 'package:asmore/library.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class PlayListMenu extends StatelessWidget {
  const PlayListMenu({super.key});

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<String>(
      icon: Icon(Icons.queue_music),
      tooltip: "播放列表",
      onSelected: (String value) {
        // 处理选中的菜单项
        print('Selected: $value');
      },
      itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
        PopupMenuItem(
          value: 'Option 1',
          child: Text('Option 1'),
        ),
        PopupMenuItem(
          value: 'Option 2',
          child: Text('Option 2'),
        ),
        PopupMenuItem(
          value: 'Option 3',
          child: Text('Option 3'),
        ),
      ],
    );
  }
}

class VolumeMenu extends StatefulWidget {
  void Function(double)? onSetVolume;

  VolumeMenu({super.key, required this.onSetVolume});

  @override
  State<VolumeMenu> createState() => _VolumeMenuState();
}

class _VolumeMenuState extends State<VolumeMenu> {
  OverlayEntry? _overlayEntry;

  ValueNotifier<double> _volume = ValueNotifier(0.4);

  var _show = false;

  void _buildOverlay(BuildContext context) {
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return Positioned(
          left: 100,
          top: 100,
          width: 200,
          height: 50,
          child: Card(
              child: Slider(
            value: _volume.value,
            min: 0.0,
            max: 1.0,
            onChanged: _seekTo,
          )),
        );
      },
    );
  }

  void _showOverlay(BuildContext context) {
    if (!_show) {
      Overlay.of(context)!.insert(_overlayEntry!);
      _show = true;
    } else {
      _overlayEntry!.remove();
      _show = false;
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _buildOverlay(context);

    return IconButton(
      icon: Icon(Icons.volume_up),
      tooltip: "音量",
      onPressed: () {
        _showOverlay(context);
        print(_show);
      },
    );
  }

  void _seekTo(double value) {
    print("set volume$value");
    setState(() {
      _volume.value = value;
    });
    widget.onSetVolume!(value);
  }
}
